repeater控件实现 编辑 删除等 操作

<ItemTemplate>
    <asp:Panel ID="plItem" runat="server">
        <tr>
            <td style="background-color: #FFF; height: 25px; width: 30%;">
                <%# DataBinder.Eval(Container.DataItem, "a")%>
            </td>
            <td style="background-color: #FFF; width: 30%;">
                <%# DataBinder.Eval(Container.DataItem,"b") %>
            </td>
            <td style="background-color: #FFF; width: 30%;">
                <%# DataBinder.Eval(Container.DataItem,"c") %>
            </td>
            <td style="background-color: #FFF; width: 10%;">
                <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                     CommandName="Edit" Text="编辑"></asp:LinkButton>&nbsp;&nbsp;&nbsp;
                <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                     CommandName="Delete" Text="删除"></asp:LinkButton>
            </td>
        </tr>
    </asp:Panel>
    <asp:Panel ID="plEdit" runat="server">
        <tr>
            <td style="background-color: #FFF; height: 25px; width: 30%;">
                <asp:TextBox ID="txtA" Text='<%# DataBinder.Eval(Container.DataItem,"a") %>' runat="server"></asp:TextBox>
            </td>
            <td style="background-color: #FFF; width: 30%;">
                <asp:TextBox ID="txtB" Text='<%# DataBinder.Eval(Container.DataItem,"b") %>' runat="server"></asp:TextBox>
            </td>
            <td style="background-color: #FFF; width: 30%;">
                <asp:TextBox ID="txtC" Text='<%# DataBinder.Eval(Container.DataItem,"c") %>' runat="server"></asp:TextBox>
            </td>
            <td style="background-color: #FFF; width: 80px; width: 10%;">
                <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                     CommandName="Update" Text="更新"></asp:LinkButton>&nbsp;&nbsp;&nbsp;
                <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>'
                     CommandName="Cancel" Text="取消"></asp:LinkButton>
            </td>
        </tr>
    </asp:Panel>
</ItemTemplate>


/////////////////////////////////////////////////////////////////////////////////////////////////////////


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if (m_dt.Rows[e.Item.ItemIndex]["ID"].ToString() != m_iID.ToString())
        {
             ((Panel)e.Item.FindControl("plItem")).Visible = true;
             ((Panel)e.Item.FindControl("plEdit")).Visible = false;
         }
        else
        {
             ((Panel)e.Item.FindControl("plItem")).Visible = false;
             ((Panel)e.Item.FindControl("plEdit")).Visible = true;
         }
     }
}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
         m_iID = int.Parse(e.CommandArgument.ToString());
     }
    else if (e.CommandName == "Cancel")
    {
         m_iID = -1;
     }
    else if (e.CommandName == "Update")
    {
        //Update.

        string sA = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtA")).Text.Trim();
        string sB = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtB")).Text.Trim();
        string sC = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("txtC")).Text.Trim();

        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" +
             e.CommandArgument + ";页面值:A," + sA + "----B," + sB + "----C," + sC + "');", true);
     }
    else if (e.CommandName == "Delete")
    {
        //Delete.            
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
     }

     Repeater1.DataSource = this.InitDataTable();
     Repeater1.DataBind();
}
posted @ 2008-11-14 13:05  Edward Xie  阅读(4848)  评论(3编辑  收藏  举报